Ext.define('Lms.rms.TimeConfigPanel',{
	extend : 'Ext.panel.Panel',
	initComponent : function(){
		Ext.apply(this,{
			id : 'TimeConfigPanel',
			title : LocaleUtil.getMessage('rms.menu.timeConf'),
			layout : 'border',
			borad : false,
			items : [
			    this.getZoneGrid(),
			    this.getSystemGrid()
			]
		});
		this.callParent(arguments);
	},
	
	getZoneGrid : function(){
		Ext.define('ZoneTimeModel', {
	        extend: 'Ext.data.Model',	        
	        fields: ['zoneId','zoneName','startTime', 'endTime'],
	    });
		var zoneTimeConfigStore = Ext.create('Ext.data.Store',{
			model : 'ZoneTimeModel',
			autoLoad : true,
			proxy : {
				url : 'schedule/getZoneList.do',
				type : 'ajax',
				reader : {
					type : 'json'
				}
			}
		});
		this.zoneGrid = Ext.create('Ext.grid.Panel',{
			region : 'center',
			store : zoneTimeConfigStore,
			borad : false,
			columnLines: true,
			height : '40%',
			viewConfig : Common.viewConfig(true),
			plugins : [Ext.create('Ext.grid.plugin.CellEditing')],
			columns : [
			    {text : LocaleUtil.getMessage('rms.label.category'),dataIndex : 'zoneName', width:200},
			    {text : LocaleUtil.getMessage('rms.label.startTime'),dataIndex : 'startTime', editor : {
			    	xtype : 'timefield',
			    	minValue : '00:00',
			    	maxValue : '24:00',
			    	increment : 10,
			    	format : 'H:i'
			    }, renderer : this.timeRender},
			    {text : LocaleUtil.getMessage('rms.label.endTime'),dataIndex : 'endTime', editor : {
			    	xtype : 'timefield',
			    	minValue : '00:00',
			    	maxValue : '24:00',
			    	increment : 10,
			    	format : 'H:i'
			    }, renderer : this.timeRender}
			],
			tbar : ['->',{
				text : LocaleUtil.getMessage('rms.label.addZone'),
				iconCls : 'add',
				scope : this,
				handler : function(){
					Ext.create('Lms.rms.ZoneInfoDialog').openWindow();
				}
			},{
				text : LocaleUtil.getMessage('save'),
				iconCls : 'save',
				scope : this,
				handler : this.onSaveForZoneTime
			}]
		});
		this.zoneGrid.on('selectionchange',this.onSelectionChangeHandler,this);
		this.zoneGrid.on('beforeedit', function(editor, context, eOpts){
			if(context.field == 'endTime'){
				if(!context.record.get('startTime')){
//					return false;
				}else{
					var startTime = context.record.get('startTime');
					if(typeof(startTime) != 'string'){
//						var today = new Date();
//						var timeArr = startTime.split('[:]');
//						startTime = new Date(today.getFullYear(),today.getMonth(),today.getDay(),timeArr[0],timeArr[1]);
//						alert(startTime);
						startTime = this.timeRender(startTime);
					}
					context.column.getEditor(context.record,'endTime').setMinValue(startTime);
				}
			}
		},this);
		return this.zoneGrid;
	},
	
	timeRender : function(value){
		if(typeof(value) == 'string'){
			return value;
		}else{
			return Ext.Date.format(value, 'H:i');
		}
	},
	
	onSaveForZoneTime : function(){
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToSave'),function(btn){
			if(btn == 'yes'){
				var updatedRecords = this.zoneGrid.getStore().getUpdatedRecords();
				var param = '?';
				for(var i in updatedRecords){
					var record = updatedRecords[i];
					param += 'scheduleVOs['+i+'].zoneId='+record.get('zoneId')+'&' +
		 	    	'scheduleVOs['+i+'].startTime='+this.timeRender(record.get('startTime'))+'&' +
		 	    	'scheduleVOs['+i+'].endTime='+this.timeRender(record.get('endTime'))+'&';
				}
				
				Ext.Ajax.request({
			        url:'schedule/updateZone.do' + param,
			        scope : this,
			        success : function(result, request){
			        	this.zoneGrid.getStore().reload();
			        },
			        failure: Common.failureMessage
			    });
			}
		},this);
	},
	
	onSelectionChangeHandler : function(selModel, selections){
		if(selections.length > 0){
			this.systemGrid.queryById('zoneLabel').setText(selections[0].get('zoneName'));
			this.systemGrid.expand();
			this.systemTimeConfigStore.load({
				params : {
					zoneId : selections[0].get('zoneId')
				}
			});
		}
	},
	
	getSystemGrid : function(){
		Ext.define('SystemTimeModel', {
	        extend: 'Ext.data.Model',	        
	        fields: ['pcId','pcName','startTime', 'endTime']
	    });
		this.systemTimeConfigStore = Ext.create('Ext.data.Store',{
			model : 'SystemTimeModel',
			proxy : {
		    	url : 'schedule/getZonePcList.do',
		    	type : 'ajax',
				reader : {
					type : 'json'
				}
		    }
		});
		this.systemGrid = Ext.create('Ext.grid.Panel',{
			region : 'south',
			store : this.systemTimeConfigStore,
			borad : false,
			columnLines: true,
			height : '60%',
			collapseMode : 'mini',
			collapsed : true,
			viewConfig : Common.viewConfig(true),
			plugins : [Ext.create('Ext.grid.plugin.CellEditing')],
			columns : [
			    {text : LocaleUtil.getMessage('rms.label.category'),dataIndex : 'pcName', width:200},
			    {text : LocaleUtil.getMessage('rms.label.startTime'),dataIndex : 'startTime', editor : {
			    	xtype : 'timefield',
			    	minValue : '00:00',
			    	maxValue : '24:00',
			    	increment : 10,
			    	format : 'H:i'
			    }, renderer : this.timeRender},
			    {text : LocaleUtil.getMessage('rms.label.endTime'),dataIndex : 'endTime', editor : {
			    	xtype : 'timefield',
			    	minValue : '00:00',
			    	maxValue : '24:00',
			    	increment : 10,
			    	format : 'H:i'
			    }, renderer : this.timeRender}
			],
			tbar : [{
				itemId : 'zoneLabel',
				xtype : 'label',				
				text : ''
			},'->',{
				text : LocaleUtil.getMessage('save'),
				iconCls : 'save',
				scope : this,
				handler : this.onSaveForPcTime
			}]
		});
		return this.systemGrid;
	},
	
	onSaveForPcTime : function(){
		Common.confirm(LocaleUtil.getMessage('confirm'),LocaleUtil.getMessage('rms.msg.doYouWantToSave'),function(btn){
			if(btn == 'yes'){
				var updatedRecords = this.systemGrid.getStore().getUpdatedRecords();
				var param = '?';
				for(var i in updatedRecords){
					var record = updatedRecords[i];
					param += 'scheduleVOs['+i+'].pcId='+record.get('pcId')+'&' +
		 	    	'scheduleVOs['+i+'].startTime='+this.timeRender(record.get('startTime'))+'&' +
		 	    	'scheduleVOs['+i+'].endTime='+this.timeRender(record.get('endTime'))+'&';
				}
				
				Ext.Ajax.request({
			        url:'schedule/updateZonePc.do' + param,
			        scope : this,
			        success : function(result, request){
			        	this.systemGrid.getStore().reload();
			        },
			        failure: Common.failureMessage
			    });
			}
		},this);
	},
});